import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


public class Outils {

	public static M chargerRegles(String path)
	{
		int count = 0;
		M matrice = new M();
		String data = chargerFichier(path);
		String[] lines = data.split("\n");
		for(int i=0;i<lines.length; i++)
		{
			String[] terms = lines[i].split(",");
			if(terms.length == 5)
			{
				V vecteur = new V();
				for(int j=0; j<terms.length; j++)
				{
					vecteur.ajout_integer(D.getInstance().code(terms[j]));
				}
				matrice.add_Vect_L(vecteur);
				count++;
			}
			else
			{
				System.out.println("Erreur durant chargement de la regle line "+ (i+1));
			}
			
		}
		System.out.println("Chargement reussi de " + count+" regles");
		return matrice;
	}
	
	public static M chargerFaits(String path)
	{
		int count = 0;
		M matrice = new M();
		String data = chargerFichier(path);
		String[] lines = data.split("\n");
		for(int i=0;i<lines.length; i++)
		{
			String[] terms = lines[i].split(",");
			if(terms.length == 3)
			{
				V vecteur = new V();
				for(int j=0; j<terms.length; j++)
				{
					vecteur.ajout_integer(D.getInstance().code(terms[j]));
				}
				matrice.add_Vect_L(vecteur);
				count++;
			}
			else
			{
				System.out.println("Erreur durant chargement du fait ligne "+ (i+1));
			}
		}
		System.out.println("Chargement reussi de " + count+" faits");
		return matrice;
	}
	
	@SuppressWarnings("deprecation")
	private static String chargerFichier(String path)
	{
		File file = new File(path);
	    FileInputStream fis = null;
	    BufferedInputStream bis = null;
	    DataInputStream dis = null;
	    StringBuffer sb = new StringBuffer();
	    
	    try {
	      fis = new FileInputStream(file);
	      bis = new BufferedInputStream(fis);
	      dis = new DataInputStream(bis);
	      
	      while (dis.available() != 0) {
	        sb.append(dis.readLine());
	        sb.append("\n");
	      }

	      fis.close();
	      bis.close();
	      dis.close();

	    } catch (FileNotFoundException e) {
	      e.printStackTrace();
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	    return sb.toString();
	}
}
